<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="700"
		destroy-on-close
		append-to-body
		@closed="emits('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			ref="dialogForm"
			label-width="80px"
			:style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
		>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="商品" prop="goods_id">
						<el-select v-model="form.goods_id" style="width:100%" clearable placeholder="请选择商品">

							<el-option
								v-for="(item, index) in dictData.wmsMdGoods"
								:key="index" :label="item.shp_ming_cheng"
								:value="item.id"
							>{{ item.shp_ming_cheng }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="库位" prop="md_bin_id">
						<el-select v-model="form.md_bin_id" style="width:100%" clearable placeholder="请选择库位">

							<el-option
								v-for="(item, index) in dictData.wmsMdBin"
								:key="index" :label="item.ku_wei_bian_ma"
								:value="item.id"
							>{{ item.ku_wei_bian_ma }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="客户" prop="cus_code">
						<el-select v-model="form.cus_code" style="width:100%" clearable placeholder="请选择客户">

							<el-option
								v-for="(item, index) in dictData.wmsMdCus"
								:key="index" :label="item.zhong_wen_qch"
								:value="item.id"
							>{{ item.zhong_wen_qch }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="预计到货时间" prop="im_data">
						<el-date-picker
							v-model="form.im_data" placeholder="请选择预计到货时间"
							type="datetime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="订单类型" prop="order_type_code">
						<el-select v-model="form.order_type_code" style="width:100%" clearable placeholder="请选择订单类型">

							<el-option
								v-for="(item, index) in dictData.wms_order_type"
								:key="index" :label="item.label"
								:value="item.value"
							>{{ item.label }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="月台" prop="platform_code">
						<el-select v-model="form.platform_code" style="width:100%" clearable placeholder="请选择月台">

							<el-option
								v-for="(item, index) in dictData.wmsBaPlatform"
								:key="index" :label="item.platform_name"
								:value="item.id"
							>{{ item.platform_name }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="车号" prop="im_car_no">
						<el-input v-model="form.im_car_no" clearable placeholder="请输入车号"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="司机" prop="im_car_dri">
						<el-input v-model="form.im_car_dri" clearable placeholder="请输入司机"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="司机电话" prop="im_car_mobile">
						<el-input v-model="form.im_car_mobile" clearable placeholder="请输入司机电话"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="客户订单号" prop="im_cus_code">
						<el-input v-model="form.im_cus_code" clearable placeholder="请输入客户订单号"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="单据状态" prop="im_sta">
						<el-switch v-model="form.im_sta" :active-value="proxy.$CONFIG.SWITCH.ENABLE"
											 :inactive-value="proxy.$CONFIG.SWITCH.DISABLE"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="进货通知单号" prop="notice_id">
						<el-input v-model="form.notice_id" clearable placeholder="请输入进货通知单号"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="附件" prop="appendix">
						<sc-upload v-model="form.appendix" title="选择附件" file-select/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="" prop="READ_ONLY">
						<el-input v-model="form.READ_ONLY" clearable placeholder="请输入"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="" prop="WHERE_CON">
						<el-input v-model="form.WHERE_CON" clearable placeholder="请输入"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="供应商编码" prop="sup_code">
						<el-input v-model="form.sup_code" clearable placeholder="请输入供应商编码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="供应商名称" prop="sup_name">
						<el-input v-model="form.sup_name" clearable placeholder="请输入供应商名称"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="对接单据类型" prop="pi_class">
						<el-input v-model="form.pi_class" clearable placeholder="请输入对接单据类型"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="账套" prop="pi_master">
						<el-input v-model="form.pi_master" clearable placeholder="请输入账套"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="状态" prop="status">
						<el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE"
											 :inactive-value="proxy.$CONFIG.SWITCH.DISABLE"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="排序" prop="sort">
						<el-input v-model="form.sort" clearable placeholder="请输入排序"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="备注" prop="im_beizhu">
						<el-input v-model="form.im_beizhu" clearable placeholder="请输入备注"/>
					</el-form-item>
				</el-col>
			</el-row>


		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import {ref, reactive, defineExpose, onMounted, getCurrentInstance} from 'vue'
import {useRouter} from "vue-router";
import {ElMessage} from 'element-plus'
import wmsWmImNoticeH from '@/api/apis/wms/wmsWmImNoticeH'
import wmsMdGoods from '@/api/apis/wms/wmsMdGoods'
import wmsMdBin from '@/api/apis/wms/wmsMdBin'
import wmsMdCus from '@/api/apis/wms/wmsMdCus'
import wmsBaPlatform from '@/api/apis/wms/wmsBaPlatform'
import systemDict from '@/api/apis/system/dataDict'

import wmsBaStore from '@/api/apis/wms/wmsBaStore'


const emits = defineEmits(['success', 'closed'])
const {proxy} = getCurrentInstance();
const router = useRouter();

const mode = ref('add')
const treeList = ref([])
const visible = ref(false)
const isSaveing = ref(false)
const dialogForm = ref(null)

const titleMap = reactive({add: '新增入库', edit: '编辑入库'})
const dictData = reactive({
	wms_order_type: [],
	platform_data_status: [],
	wmsMdGoods:[],
	wmsMdBin:[],
	wmsMdCus:[],
	wmsBaPlatform:[],
})
const form = reactive({
	id: '',
	goods_id: '',
	md_bin_id: '',
	cus_code: '',
	im_data: null,
	im_cus_code: '',
	im_car_dri: '',
	im_car_mobile: '',
	im_car_no: '',
	order_type_code: '2',
	platform_code: '',
	im_sta: proxy.$CONFIG.SWITCH.ENABLE,
	notice_id: '',
	appendix: '',
	READ_ONLY: '',
	WHERE_CON: '',
	sup_code: '',
	sup_name: '',
	pi_class: '',
	pi_master: '',
	status: proxy.$CONFIG.SWITCH.ENABLE,
	sort: '0',
	mer_id: '0',
	im_beizhu: '',

})
const rules = reactive({
	goods_id: [{required: true, message: '商品必填', trigger: 'blur'}],
	md_bin_id: [{required: true, message: '库位必填', trigger: 'blur'}],
	cus_code: [{required: true, message: '客户必填', trigger: 'blur'}],
	im_data: [{required: true, message: '预计到货时间必填', trigger: 'blur'}],
	im_car_no: [{required: true, message: '车号必填', trigger: 'blur'}],
	order_type_code: [{required: true, message: '订单类型必填', trigger: 'blur'}],
	platform_code: [{required: true, message: '月台必填', trigger: 'blur'}],

})

onMounted(async () => {
	await getDictData()
	if (router.currentRoute.value.query.open) {
		open('add');
	}
})

const getDictData = () => {
	systemDict.getDict('wms_order_type').then(res => {
		dictData.wms_order_type = res.data
	})
	systemDict.getDict('platform_data_status').then(res => {
		dictData.platform_data_status = res.data
	})

	// 商品
	wmsMdGoods.list().then(res => {
		dictData.wmsMdGoods = res.data
	})
	// 库位
	wmsMdBin.list().then(res => {
		dictData.wmsMdBin = res.data
	})
	// 客户
	wmsMdCus.list().then(res => {
		dictData.wmsMdCus = res.data
	})
	// 月台
	wmsBaPlatform.list().then(res => {
		dictData.wmsBaPlatform = res.data
	})
}

const open = (type = 'add') => {
	mode.value = type
	visible.value = true

}

const submit = () => {
	dialogForm.value.validate(async (valid) => {
		if (valid) {
			isSaveing.value = true
			const res = mode.value === 'add' ? await wmsWmImNoticeH.save(form) : await wmsWmImNoticeH.update(form.id, form)
			if (res.success) {
				emits('success', form, mode.value)
				visible.value = false
				ElMessage.success(res.message)
			} else {
				ElMessage.error(res.message)
			}
			isSaveing.value = false
		}
	})
}

const setData = (data) => {
	for (let k in form) {
		if (data[k] || data[k] === 0) {
			form[k] = data[k]
		}
	}
}


defineExpose({
	open, setData
})
</script>
